﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class bas_control : BasePermissionPage
{
    SqlDataSource SDS_CONTROL = new SqlDataSource();
    DataView dv = new DataView();
    DataView dv_control_type = new DataView();

    protected void Page_Load(object sender, EventArgs e)
    {
        //== 1.連結資料庫的連接字串 ConnectionString  ，事先已經寫在 Web.Config檔案裡面了==
        SDS_CONTROL.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SDS_CONTROL.ProviderName = "Oracle.DataAccess.Client";
        SDS_CONTROL.CancelSelectOnNullParameter = false;
        if (!IsPostBack)
        {
            //DBINIT();
        }
        else
        {

        }
    }
    protected void DBINIT()
    {
        try
        {
            string v_CONTROL_ID, v_SQL, v_CONTROL_NAME, v_CONTROL_TYPE;
            DataSourceSelectArguments args = new DataSourceSelectArguments();



            v_CONTROL_ID = txtCONTROL_ID.Text.Trim();
            v_CONTROL_NAME = txtCONTROL_NAME.Text.Trim();
            v_CONTROL_TYPE = ddlCONTROL_TYPE.SelectedValue;
            //== 2.撰寫SQL指令 ==
            v_SQL = "SELECT CONTROL_ID,CONTROL_NAME,CONTROL_TYPE  FROM MIS_CONTROL_AP ";
            v_SQL = v_SQL + " Where ( :CONTROL_ID is null or CONTROL_ID = :CONTROL_ID) and (CONTROL_NAME like :CONTROL_NAME) and (:CONTROL_TYPE is null or CONTROL_TYPE = :CONTROL_TYPE)";
            v_SQL = v_SQL + " order by CONTROL_ID ";
            SDS_CONTROL.SelectParameters.Clear();
            SDS_CONTROL.SelectCommand = v_SQL;
            SDS_CONTROL.SelectParameters.Add("CONTROL_ID", TypeCode.String, v_CONTROL_ID);
            SDS_CONTROL.SelectParameters.Add("CONTROL_ID", TypeCode.String, v_CONTROL_ID);
            SDS_CONTROL.SelectParameters.Add("CONTROL_NAME", TypeCode.String, "%" + v_CONTROL_NAME + "%");
            SDS_CONTROL.SelectParameters.Add("CONTROL_TYPE", TypeCode.String, v_CONTROL_TYPE);
            SDS_CONTROL.SelectParameters.Add("CONTROL_TYPE", TypeCode.String, v_CONTROL_TYPE);

            //== 3.執行SQL指令 .select() ==
            SDS_CONTROL.DataSourceMode = SqlDataSourceMode.DataSet;

            dv = (DataView)SDS_CONTROL.Select(args);
            //dv.AddNew();
            DV1.DataSource = dv;


            DV1.DataBind();

            
        }
        catch (Exception ex)
        {
            //Response.Write(e.Message);
            lbl_Message.Text = ex.Message;
            ERROR_DBSAVE("bas_control.aspx.cs", "DBINIT", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    protected void DV1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {

            DV1.EditIndex = e.NewEditIndex;
            DBINIT();
        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
            ERROR_DBSAVE("bas_control.aspx.cs", "DV1_RowEditing", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    protected void DV1_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        try
        {
            lbl_Message.Text = "";
            DV1.EditIndex = -1;
            DBINIT();
        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
            ERROR_DBSAVE("bas_control.aspx.cs", "DV1_ItemCanceling", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    protected void DV1_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        try
        {

            DV1.EditIndex = e.NewEditIndex;
            DBINIT();
        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
            ERROR_DBSAVE("bas_control.aspx.cs", "DV1_ItemEditing", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    protected void DV1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int index;
        string v_CONTROL_ID, v_CONTROL_NAME, v_CONTROL_TYPE;
        TextBox TB = new TextBox();
        try
        {
            index = e.ItemIndex;
            v_CONTROL_ID = (string)DV1.DataKeys[index].Value;
            // EmptyDataTemplate
            v_CONTROL_NAME = ((TextBox)DV1.EditItem.FindControl("txtCONTROL_NAME_G")).Text.Trim();
            DropDownList ddlCONTROL_TYPE_G = (DropDownList)DV1.EditItem.FindControl("ddlCONTROL_TYPE_G");
            v_CONTROL_TYPE = ddlCONTROL_TYPE_G.SelectedValue;
            TB = ((TextBox)DV1.EditItem.FindControl("txtCONTROL_TYPE"));
            lbl_Message.Text = string.Empty;
            if (check(v_CONTROL_TYPE) == false)
            {
                lbl_Message.Text = "error!";
                ddlCONTROL_TYPE_G.Focus();
                e.Cancel = true;
            }
            else
            {

                //'==下面的程式，只是作SQL指令的更新動作而已
                //'== 設定SQL指令將會用到的參數 ==
                SDS_CONTROL.SelectParameters.Clear();
                SDS_CONTROL.UpdateParameters.Add("CONTROL_NAME", v_CONTROL_NAME);
                SDS_CONTROL.UpdateParameters.Add("CONTROL_TYPE", v_CONTROL_TYPE);
                SDS_CONTROL.UpdateParameters.Add("UPD_ID", User.Identity.Name);
                SDS_CONTROL.UpdateParameters.Add("UPD_DATE", System.DateTime.Now.ToString("yyyyMMdd"));
                SDS_CONTROL.UpdateParameters.Add("UPD_TIME", System.DateTime.Now.ToString("HHmmss"));
                SDS_CONTROL.UpdateParameters.Add("CONTROL_ID", v_CONTROL_ID);

                SDS_CONTROL.UpdateCommand = "UPDATE MIS_CONTROL_AP SET CONTROL_NAME = :CONTROL_NAME, CONTROL_TYPE = :CONTROL_TYPE, UPD_ID = :UPD_ID, UPD_DATE = :UPD_DATE, UPD_TIME = :UPD_TIME WHERE CONTROL_ID = :CONTROL_ID";
                SDS_CONTROL.Update();
                //'==========================================
                //'==== 離開編輯模式 ====
                DV1.EditIndex = -1;
                DBINIT();
            }

        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
            ERROR_DBSAVE("bas_control.aspx.cs", "DV1_ItemUpdating", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    protected void DV1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int index;
        string v_CONTROL_ID;
        try
        {
            index = e.ItemIndex;
            v_CONTROL_ID = (string)DV1.DataKeys[index].Value;

            //'==下面的程式，只是作SQL指令的更新動作而已
            //'== 設定SQL指令將會用到的參數 ==
            SDS_CONTROL.DeleteParameters.Add("CONTROL_ID", v_CONTROL_ID);
            SDS_CONTROL.DeleteCommand = "Delete MIS_CONTROL_AP Where CONTROL_ID = :CONTROL_ID";
            SDS_CONTROL.Delete();
            DBINIT();

        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
            ERROR_DBSAVE("bas_control.aspx.cs", "DV1_ItemDeleting", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        var controlItem = new List<ControlItem>() 
        {
          new ControlItem("ddlCONTROL_TYPE", ddlCONTROL_TYPE.SelectedValue, ddlCONTROL_TYPE.GetType()),
          new ControlItem("txtCONTROL_ID", txtCONTROL_ID.Text, txtCONTROL_ID.GetType()),
          new ControlItem("txtCONTROL_NAME", txtCONTROL_NAME.Text, txtCONTROL_NAME.GetType())      
        };
        ConditionRecordUtil.RecordSearchConditionData(controlItem);

        DBINIT();
        lbl_Message.Text = string.Empty;
        int PageSize = ((DataPager)DV1.FindControl("DataPager1")).PageSize;
        ((DataPager)DV1.FindControl("DataPager1")).SetPageProperties(0, PageSize, true);
        
    }
    protected void DV1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        string v_CONTROL_ID, v_CONTROL_NAME, v_CONTROL_TYPE;
        TextBox TB = new TextBox();
        try
        {
            v_CONTROL_ID = ((TextBox)DV1.InsertItem.FindControl("txtCONTROL_ID_G")).Text.Trim();
            // EmptyDataTemplate
            v_CONTROL_NAME = ((TextBox)DV1.InsertItem.FindControl("txtCONTROL_NAME_G")).Text.Trim();
            DropDownList ddlCONTROL_TYPE_G = (DropDownList)DV1.InsertItem.FindControl("ddlCONTROL_TYPE_G");
            v_CONTROL_TYPE = ddlCONTROL_TYPE_G.SelectedValue;

            lbl_Message.Text = string.Empty;
            if (check(v_CONTROL_TYPE) == false)
            {
                lbl_Message.Text = "Error";
                ddlCONTROL_TYPE_G.Focus();
                e.Cancel = true;
            }
            else
            {

                //'==下面的程式，只是作SQL指令的更新動作而已
                //'== 設定SQL指令將會用到的參數 ==
                SDS_CONTROL.InsertParameters.Clear();

                SDS_CONTROL.InsertParameters.Add("CONTROL_ID", v_CONTROL_ID);
                SDS_CONTROL.InsertParameters.Add("CONTROL_NAME", v_CONTROL_NAME);
                SDS_CONTROL.InsertParameters.Add("CONTROL_TYPE", v_CONTROL_TYPE);
                SDS_CONTROL.InsertParameters.Add("CRT_ID", User.Identity.Name);
                SDS_CONTROL.InsertParameters.Add("CRT_DATE", System.DateTime.Now.ToString("yyyyMMdd"));
                SDS_CONTROL.InsertParameters.Add("CRT_TIME", System.DateTime.Now.ToString("HHmmss"));
                SDS_CONTROL.InsertParameters.Add("UPD_ID", User.Identity.Name);
                SDS_CONTROL.InsertParameters.Add("UPD_DATE", System.DateTime.Now.ToString("yyyyMMdd"));
                SDS_CONTROL.InsertParameters.Add("UPD_TIME", System.DateTime.Now.ToString("HHmmss"));


                SDS_CONTROL.InsertCommand = "insert into mis_CONTROL_ap (CONTROL_id, CONTROL_name, CONTROL_TYPE, crt_id, crt_date, crt_time, upd_id, upd_date, upd_time) values(:CONTROL_id, :CONTROL_name, :CONTROL_TYPE, :crt_id, :crt_date, :crt_time, :upd_id, :upd_date, :upd_time)";
                SDS_CONTROL.Insert();
                //'==========================================
                DBINIT();
            }

        }

        catch (Exception ex)
        {
            //Response.Write(ex.Message);
            //PK重覆
            if (ex.Message.IndexOf("ORA-00001") >= 0)
            {
                lbl_Message.Text = "輸入的公司代號已存在公司檔中！";
            }
            else
            {
                lbl_Message.Text = ex.Message;
            }
            ERROR_DBSAVE("bas_control.aspx.cs", "DV1_ItemInserting", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    protected void btn_exit_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
    protected void DV1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        DV1.SelectedIndex = -1;

    }
    protected void DV1_PagePropertiesChanged(object sender, EventArgs e)
    {
        int PageSize = ((DataPager)DV1.FindControl("DataPager1")).PageSize;
        int StartRowIndex = ((DataPager)DV1.FindControl("DataPager1")).StartRowIndex;
        int TotalRowCount = ((DataPager)DV1.FindControl("DataPager1")).TotalRowCount;
        ((DataPager)DV1.FindControl("DataPager1")).SetPageProperties(StartRowIndex, PageSize, true);
        DBINIT();
    }

    protected Boolean check(string p_CONTROL_TYPE)
    {
        //判斷CONTROL_TYPE必需存在CONTROL_ID
        
        return true;
    }
    protected void roleddl_DataBound(object sender, ListViewItemEventArgs e)
    {
        string v_CONTROL_Type="Button";
        ListItem listItem;
        //v_CONTROL_Type = ((TextBox)DV1.InsertItem.FindControl("txtCONTROL_TYPE")).Text;
        DropDownList ddlCONTROL_TYPE_G = (DropDownList)sender;
        ddlCONTROL_TYPE_G.ClearSelection();
        listItem = ddlCONTROL_TYPE_G.Items.FindByValue(v_CONTROL_Type);
        listItem.Selected = true;
    }
    protected void DV1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        DropDownList pid = (DropDownList)e.Item.FindControl("ddlCONTROL_TYPE_G");
        if (pid == null) return;
        string selectedValue = (e.Item.FindControl("txtCONTROL_TYPE") as TextBox).Text;
        pid.SelectedValue = selectedValue;
    }
}